package com.googlecode.gwt.wii.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.gwt.wii.wiimote.Wiimote;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTWii implements EntryPoint {
	private Button clickMeButton;
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();

		final FlexTable flexTable = new FlexTable();
		rootPanel.add(flexTable, 14, 75);
		flexTable.setSize("472px", "300px");

		final Label isenabledLabel = new Label("isEnabled");
		flexTable.setWidget(0, 0, isenabledLabel);

		final Label labelIsEnabled = new Label("");
		flexTable.setWidget(0, 1, labelIsEnabled);

		final Label isdatavalidLabel = new Label("isDataValid");
		flexTable.setWidget(1, 0, isdatavalidLabel);

		final Label labelIsBrowsing = new Label("");
		flexTable.setWidget(2, 1, labelIsBrowsing);

		final Label isbrowsingLabel = new Label("isBrowsing");
		flexTable.setWidget(2, 0, isbrowsingLabel);

		final Label dpdscreenxLabel = new Label("dpdScreenX");
		flexTable.setWidget(3, 0, dpdscreenxLabel);

		final Label labelDpdScreenY = new Label("");
		flexTable.setWidget(4, 1, labelDpdScreenY);

		final Label dpdscreenyLabel = new Label("dpdScreenY");
		flexTable.setWidget(4, 0, dpdscreenyLabel);

		final Label labelDpdScreenX = new Label("");
		flexTable.setWidget(3, 1, labelDpdScreenX);

		final Label dpdxLabel = new Label("dpdX");
		flexTable.setWidget(5, 0, dpdxLabel);

		final Label labelDpdY = new Label("");
		flexTable.setWidget(6, 1, labelDpdY);

		final Label dpdyLabel = new Label("dpdY");
		flexTable.setWidget(6, 0, dpdyLabel);

		final Label labelDpdX = new Label("");
		flexTable.setWidget(5, 1, labelDpdX);

		final Label holdLabel = new Label("hold");
		flexTable.setWidget(7, 0, holdLabel);

		final Label dpdrollxLabel = new Label("dpdRollX");
		flexTable.setWidget(8, 0, dpdrollxLabel);

		final Label dpdrollyLabel = new Label("dpdRollY");
		flexTable.setWidget(9, 0, dpdrollyLabel);

		final Label dpddistanceLabel = new Label("dpdDistance");
		flexTable.setWidget(10, 0, dpddistanceLabel);

		final Label dpdvalidityLabel = new Label("dpdValidity");
		flexTable.setWidget(11, 0, dpdvalidityLabel);

		final Label labelIsDataValid = new Label("");
		flexTable.setWidget(1, 1, labelIsDataValid);

		final Label labelHold = new Label("");
		flexTable.setWidget(7, 1, labelHold);
		
		final Label labelDpdRollX = new Label("");
		flexTable.setWidget(8, 1, labelDpdRollX);
		
		final Label labelDpdRollY = new Label("");
		flexTable.setWidget(9, 1, labelDpdRollY);
		
		final Label labelDpdDistance = new Label("");
		flexTable.setWidget(10, 1, labelDpdDistance);
		
		final Label labelDpdValidity = new Label("");
		flexTable.setWidget(11, 1, labelDpdValidity);
		
		final Command updateCommand = new Command(){

			public void execute() {
				labelIsEnabled.setText(""+Wiimote.getWiimote(0).isEnabled());
				labelIsDataValid.setText(""+Wiimote.getWiimote(0).isDataValid());
				labelIsBrowsing.setText(""+Wiimote.getWiimote(0).isBrowsing());
				labelDpdX.setText(""+Wiimote.getWiimote(0).dpdX());
				labelDpdY.setText(""+Wiimote.getWiimote(0).dpdY());
				labelDpdScreenX.setText(""+Wiimote.getWiimote(0).dpdScreenX());
				labelDpdScreenY.setText(""+Wiimote.getWiimote(0).dpdScreenY());		
				labelHold.setText(""+Wiimote.getWiimote(0).hold());				
				labelDpdRollX.setText(""+Wiimote.getWiimote(0).dpdRollX());				
				labelDpdRollY.setText(""+Wiimote.getWiimote(0).dpdRollY());				
				labelDpdDistance.setText(""+Wiimote.getWiimote(0).dpdDistance());				
				labelDpdValidity.setText(""+Wiimote.getWiimote(0).dpdValidity());				
			}
			
		};
		
		Timer timer = new Timer() {
			public void run() {
				updateCommand.execute();
			}
		};

		timer.scheduleRepeating(100);
		
		clickMeButton = new Button();
		rootPanel.add(clickMeButton, 223, 524);
		clickMeButton.setText("Update");
		clickMeButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				updateCommand.execute();
			}
		});

		final Label ciaoJacopoSonoLabel = new Label("Ciao Jacopo, sono lo Wii !!");
		rootPanel.add(ciaoJacopoSonoLabel, 14, 24);
		ciaoJacopoSonoLabel.setWidth("290px");

	}
}
